/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.agent.tools;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.log4j.Logger;

/**
 *
 * @author G
 */
public class FileTools {
    //读取文件内容

    private static Logger logger = Logger.getLogger(FileTools.class);

    public String readFile(String filename) {
        StringBuffer sb = new StringBuffer();
        try {
            FileReader read = new FileReader(filename);
            BufferedReader br = new BufferedReader(read);
            String row = "";
            while ((row = br.readLine()) != null) {
                sb.append(row);
            }
        } catch (FileNotFoundException e) {
            logger.debug("read file error FileNotFoundException", e);
        } catch (IOException e) {
            logger.debug("read file error IOException", e);
        }
        return sb.toString();
    }

    public String readFileByStream(String filename) {
        File file = new File(filename);
        String jsonString = null;
        if (file.exists() && file.isFile()) {
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(file));
                long contentLength = file.length();
                ByteArrayOutputStream outstream = new ByteArrayOutputStream(contentLength > 0 ? (int) contentLength : 1024);
                byte[] buffer = new byte[4096];
                int len;
                while ((len = is.read(buffer)) > 0) {
                    outstream.write(buffer, 0, len);
                }
                outstream.flush();
                outstream.close();
                jsonString = new String(outstream.toByteArray(), "utf-8");
            } catch (FileNotFoundException ex) {
                logger.error(ex.getMessage(), ex);
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        return jsonString;
    }

    public void mkFile(String path, String filename, String content) {
        File file = new File(path);
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }

        try {
            FileOutputStream fos = new FileOutputStream(new File(path + filename));
            Writer os = new OutputStreamWriter(fos, "GBK");
            os.write(content);
            os.flush();
            fos.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void mkFile(String filenamePath, String content) {
        try {
            FileOutputStream fos = new FileOutputStream(new File(filenamePath));
            Writer os = new OutputStreamWriter(fos, "GBK");
            os.write(content);
            os.flush();
            fos.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void deleteFile(String filenamePath) {
        File file = new File(filenamePath);
        if (file.isFile()) {
            file.delete();
        }
    }

    public boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
}
